﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace TileSetGenerator
{
    public partial class frmMain : Form
    {
        Tileset tileset;

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnLoadImage_Click(object sender, EventArgs e)
        {
            DialogResult result = ofdOpenImage.ShowDialog();

            if (result == DialogResult.OK)
            {
                pbTileSet.ImageLocation = ofdOpenImage.FileName;
                string filename = Path.GetFileNameWithoutExtension(ofdOpenImage.FileName);
                tbTextureName.Text = filename;
            }
        }

        private void btnGenerateTileSet_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbTextureName.Text))
            {
                MessageBox.Show("You must add an image for the tile set");
                return;
            }
            tileset = new Tileset(tbTextureName.Text,
                (int)nudTileWidth.Value,
                (int)nudTileHeight.Value,
                (int)nudTilesWide.Value,
                (int)nudTilesHigh.Value);

            lbTileSetRectangles.Items.Clear();
            foreach (Microsoft.Xna.Framework.Rectangle r in tileset.tiles)
            {
                lbTileSetRectangles.Items.Add(r.ToString());
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
        }

        private void btnSaveTileSet_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbTextureName.Text))
            {
                MessageBox.Show("You must load an image.");
                return;
            }

            if (tileset == null)
            {
                MessageBox.Show("You must generate a tile set before saving.");
                return;
            }

            if (sfdSaveTileSet.ShowDialog() == DialogResult.OK)
            {

                XmlDocument xmlDoc = new XmlDocument();
                XmlElement root = xmlDoc.CreateElement("Tileset");
                xmlDoc.AppendChild(root);

                XmlElement textureElement = xmlDoc.CreateElement("TextureElement");
                root.AppendChild(textureElement);

                XmlAttribute textureAttrb = xmlDoc.CreateAttribute("TextureName");
                textureAttrb.Value = tileset.textureName;
                textureElement.Attributes.Append(textureAttrb);

                XmlElement tileDefElement = xmlDoc.CreateElement("TilesetDefinitions");
                root.AppendChild(tileDefElement);

                XmlAttribute tileDefAttrb = xmlDoc.CreateAttribute("TileWidth");
                tileDefAttrb.Value = tileset.tileWidth.ToString();
                tileDefElement.Attributes.Append(tileDefAttrb);

                tileDefAttrb = xmlDoc.CreateAttribute("TileHeight");
                tileDefAttrb.Value = tileset.tileHeight.ToString();
                tileDefElement.Attributes.Append(tileDefAttrb);

                XmlElement tileListElement = xmlDoc.CreateElement("TilesetRectangles");
                root.AppendChild(tileListElement);

                foreach (Microsoft.Xna.Framework.Rectangle rect in tileset.tiles)
                {
                    XmlElement tileRectElement = xmlDoc.CreateElement("Rectangle");

                    XmlAttribute tileAttrb = xmlDoc.CreateAttribute("X");
                    tileAttrb.Value = rect.X.ToString();
                    tileRectElement.Attributes.Append(tileAttrb);

                    tileAttrb = xmlDoc.CreateAttribute("Y");
                    tileAttrb.Value = rect.Y.ToString();
                    tileRectElement.Attributes.Append(tileAttrb);

                    tileAttrb = xmlDoc.CreateAttribute("Width");
                    tileAttrb.Value = rect.Width.ToString();
                    tileRectElement.Attributes.Append(tileAttrb);

                    tileAttrb = xmlDoc.CreateAttribute("Height");
                    tileAttrb.Value = rect.Height.ToString();
                    tileRectElement.Attributes.Append(tileAttrb);

                    tileListElement.AppendChild(tileRectElement);
                }

                try
                {
                    xmlDoc.Save(sfdSaveTileSet.FileName);
                    MessageBox.Show("Tileset saved successfully.", "Success", MessageBoxButtons.OK);
                }
                catch
                {
                    MessageBox.Show("Error writing tileset.", "Error", MessageBoxButtons.OK);
                }
            }
        }
    }
}
